<template>
  <div class="main">
    <div class="header">
      <van-icon name="arrow-left" size="30" @click="$router.go(-1)" />
      <div ref="refTitle" class="title">{{ film.name }}</div>
    </div>
    <div class="bgImage">
      <img :src="film.poster" alt="" srcset="" />
    </div>
    <div class="content">
      <div class="content-top">
        <div class="col">
          <p>{{ film.name }}</p>
          <p class="grade">{{ film.grade }}</p>
        </div>
        <div class="details">
          <p>{{ film.category }}</p>
          <p>
            {{
              new Date(film.premiereAt * 1000)
                .toLocaleDateString()
                .replaceAll("/", "-")
            }}上映
          </p>
          <p>
            {{ film.nation }} |
            {{ film.runtime }}
          </p>
          <!-- <p>{{ state.film.synopsis }}</p> -->
          <p
            id="synopsis"
            class="film-synopsis grey-text"
            :style="{
              height: hideSynopsis
                ? '55px'
                : `${Math.ceil(film.synopsis.length / 28) * 20}px`,
            }"
          >
            {{ film.synopsis }}
          </p>
          <div class="toggle" @click="hideSynopsis = !hideSynopsis">
            <!-- 隐藏时使用↓图标 -->
            <van-icon name="arrow-down" size="12" v-if="hideSynopsis" />
            <!-- 展开时使用↑图标 -->
            <van-icon name="arrow-up" size="12" v-else />
          </div>
        </div>
      </div>
      <div class="actors">
        <p>演职人员</p>

        <div class="actorList">
          <van-swipe
            :loop="false"
            :width="95"
            class="my-swipe"
            :show-indicators="false"
          >
            <van-swipe-item
              v-for="item in film.actors"
              :key="item"
              class="my-item"
            >
              <img :src="item.avatarAddress" alt="" />
              <div class="my-box">
                <span class="playCount">
                  {{ item.name }}
                </span>
                <span class="name">{{ item.role }}</span>
              </div>
            </van-swipe-item>
          </van-swipe>
        </div>
      </div>
      <div class="actors">
        <div class="actors-header">
          <p>剧照</p>
          <p class="all">全部(5) <van-icon name="arrow" size="14" /></p>
        </div>
        <div class="photoList">
          <van-swipe
            :loop="false"
            :width="180"
            class="photo-swipe"
            :show-indicators="false"
          >
            <van-swipe-item
              v-for="item in film.photos"
              :key="item"
              class="my-item"
            >
              <img :src="item" alt="" />
            </van-swipe-item>
          </van-swipe>
        </div>
        <a
          href="https://m.maizuo.com/v5/#/film/6147/cinemas"
          class=""
          style="
            height: 49px;
            position: fixed;
            bottom: 0px;
            width: 100%;
            z-index: 99;
          "
          ><div class="goSchedule">选座购票</div></a
        >
      </div>
      <div v-if="!film">
        <h1 style="padding-top: 50px; text-align: center">Loading...</h1>
      </div>
    </div>
  </div>
</template>

<script setup>
import { getDetail } from "../../api/movieApi";

import { onMounted, reactive, toRefs, nextTick, ref, onUnmounted } from "vue";
import { useRoute } from "vue-router";
const route = useRoute();
const state = reactive({
  film: [],
  titleDom: null,
  hideSynopsis: true,
});
const refTitle = ref();
const { hideSynopsis, film } = toRefs(state);

onMounted(async () => {
  const filmid = route.query.id;
  const res = await getDetail(filmid);
  console.log(res);
  state.film = res;

  //下一跳
  nextTick(() => {
    state.titleDom = refTitle.value;
    console.log(state.titleDom);
  });

  //滚动监听
  const mainDom = document.querySelector("main");
  mainDom.onscroll = (e) => {
    // 如果标题栏尚未渲染完毕 就什么都不做
    if (!state.titleDom) return;
    // 实时main元素滚动的距离
    // console.log("scroll", mainDom.scrollTop, state.titleDom);
    //如果滚动距离超过50就白底黑字，否则透明
    if (mainDom.scrollTop > 50) {
      state.titleDom.style.backgroundColor = "white";
      state.titleDom.style.color = "black";
    } else {
      state.titleDom.style.backgroundColor = "transparent";
      state.titleDom.style.color = "transparent";
    }
  };
});
//清除滚动事件
onUnmounted(() => {
  document.querySelector("main").onscroll = null;
});
</script>

<style lang="scss" scoped>
.bb {
  width: 100%;
  height: 100%;
}
.header,
.title {
  position: fixed;
  top: 0;
  left: 0;
  transition: all 0.5s ease;
  width: 100vw;
  height: 44px;
  z-index: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 18px;
  .title {
    color: transparent;
    transition: all 0.5s ease;
  }
  .van-icon {
    position: absolute;
    top: 5px;
    left: 5px;
    z-index: 999;
  }
}
.bgImage {
  width: 100%;
  height: 300px;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: top center;
  }
}
.content {
  background-color: #ececec;
  width: 100%;

  .content-top {
    width: 100%;
    background-color: #fff;
    padding: 15px;
    padding-top: 12px;
    .col {
      width: 100%;
      height: 30px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 18px;
      .grade {
        color: #ffb232;
      }
    }
    .details {
      margin-top: 4px;
      p {
        margin-top: 4px;
        font-size: 13px;
        color: #797d82;
      }
      #synopsis {
        transition: height 0.5s linear;
        overflow: hidden;
      }
      .toggle {
        text-align: center;
      }
    }
  }
}
.actors {
  margin-top: 10px;
  background-color: #fff;
  p {
    padding: 15px;
    font-size: 18px;
    color: #000;
  }
  .actors-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .all {
      font-size: 14px;
      color: #797d82;
    }
  }
  .actorList {
    width: 100%;
    .my-swipe {
      width: 100%;
      height: 140px;
      .my-item {
        padding-left: 8px;
        height: 95px;
      }

      img {
        width: 95%;
        height: 100%;
      }
    }
    .my-box {
      display: flex;
      width: 85%;
      flex-direction: column;
      align-items: center;
      .name {
        font-size: 10px;
        color: #797d82;
      }
    }
  }
  .photoList {
    width: 100%;
    padding-bottom: 5%;
    .photo-swipe {
      width: 100%;
      padding-left: 10px;
      height: 95px;
      img {
        width: 95%;
        height: 100%;
      }
    }
  }
  .goSchedule {
    position: fixed;
    bottom: 0;
    left: 0;
    height: 49px;
    width: 100%;
    text-align: center;
    background-color: #ff5f16;
    color: #fff;
    font-size: 16px;
    line-height: 49px;
  }
}
</style>
